package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.domain.MemberAddr;
import org.example.model.Result;
import org.example.service.MemberAddrService;
import org.example.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("p/address")
@Api(tags = "会员地址接口")
public class MemberAddrController {

    @Autowired
    private MemberAddrService memberAddrService;

    @ApiOperation("查询会员所有收货地址")
    @GetMapping("list")
    public Result<List<MemberAddr>> loadMemberAddrList() {
        String openId = AuthUtils.getMemberOpenId();
        List<MemberAddr> memberAddrs = memberAddrService.queryMemberAddrListByOpenId(openId);
        return Result.success(memberAddrs);
    }

    @ApiOperation("新增会员收货地址")
    @PostMapping
    public Result<String> saveMemberAddr(@RequestBody MemberAddr memberAddr) {
        String openId = AuthUtils.getMemberOpenId();
        Boolean saved = memberAddrService.saveMemberAddr(memberAddr, openId);
        return Result.handle( saved);
    }

    @ApiOperation("查询会员收货地址详情")
    @GetMapping("/addrInfo/{addrId}")
    public Result<MemberAddr> loadMemberAddrInfo(@PathVariable Long addrId) {
        MemberAddr memberAddr = memberAddrService.getById( addrId);
        return Result.success( memberAddr);
    }

    @ApiOperation("修改会员收货地址信息")
    @PutMapping
    public Result<String> modifyMemberAddrInfo(@RequestBody MemberAddr memberAddr) {
        String openId = AuthUtils.getMemberOpenId();
        Boolean modified = memberAddrService.modifyMemberAddrInfo(memberAddr, openId);
        return Result.handle( modified);
    }

    @ApiOperation("删除会员收货地址")
    @DeleteMapping("/deleteAddr/{addrId}")
    public Result<String> removeMemberAddr(@PathVariable Long addrId) {
        String openId = AuthUtils.getMemberOpenId();
        Boolean removed = memberAddrService.removeMemberAddrById(addrId, openId);
        return Result.handle( removed);
    }

    @ApiOperation("会员设置默认收货地址")
    @PutMapping("/defaultAddr/{newAddrId}")
    public Result<String> modifyMemberDefaultAddr(@PathVariable Long newAddrId) {
        String openId = AuthUtils.getMemberOpenId();
        Boolean modified = memberAddrService.modifyMemberDefaultAddr(openId, newAddrId);
        return Result.handle( modified);
    }

    //Feign接口调用
    @GetMapping("/getMemberAddrById")
    public Result<MemberAddr> getMemberAddrById(@RequestParam Long addrId) {
        MemberAddr memberAddr = memberAddrService.getById( addrId);
        return Result.success( memberAddr);
    }

    @GetMapping("getMemberDefaultAddrByOpenId")
    public Result<MemberAddr> getMemberDefaultAddrByOpenId(@RequestParam String openId){
        MemberAddr memberAddr = memberAddrService.getOne( new LambdaQueryWrapper<MemberAddr>()
                .eq(MemberAddr::getOpenId, openId)
                .eq(MemberAddr::getCommonAddr, 1));
        return Result.success( memberAddr);
    }

}
